package ThreadTest;

import java.util.Scanner;

public class TestDemo {

    volatile public static int flag = 0;//内存可见性，根本原因为编译器优化
    public static void main(String[] args) throws InterruptedException {
        Scanner scanner = new Scanner(System.in);
        Thread thread1 = new Thread(()->{
            while (flag == 0) {

            }
            System.out.println("thread1线程结束");
        });
        thread1.start();

        Thread thread2 = new Thread(()->{
            System.out.println("请输入flag的新值:");
            flag = scanner.nextInt();
        });
        thread2.start();
    }
}
